'use strict'
/**
 * errorName: {code, message, status}
 * 错误名: {错误码, 错误信息, http状态码}
 */

const defines = {
  'common.all': { code: 1000, message: '%s', status: 500 },
  'request.paramError': {code: 1001, message: '参数错误 %s', status: 200},
  'access.forbidden': { code: 1103, message: '没有操作权限,请联系开发人员!', status: 403 },
  'auth.notPermission': { code: 1100, message: '授权失败: %s', status: 400 },
  'auth.notExist': { code: 1101, message: '角色不存在,请联系开发人员!', status: 200 },
  'auth.codeExpired': { code: 1101, message: '授权码已失效,请联系开发人员重新获取!', status: 401 },
  'auth.codeError': { code: 1101, message: '授权码错误,请联系开发人员重新获取!', status: 403 },
  'auth.pargramNotExist': { code: 1100, message: '程序不存在！', status: 200 },
  'auth.pargramSecretError': { code: 1102, message: '程序秘钥错误,请联系开发人员!', status: 200 },
  'auth.pargramSecretEmpty': { code: 1102, message: '程序秘钥为空，请联系开发人员配置!', status: 200 },
  'auth.userNameInvalid': { code: 1104, message: '我不信您叫这个名字!', status: 401 },
  'auth.userHasExist' : {code: 1105, message: '用户已经存在', status: 200},
  'auth.userUpdate' : {code: 1103, message: '修改用户信息失败', status: 200},
  'auth.psderror' : {code: 1202, message: '账号或密码错误，请重新输入!', status: 200},
  'auth.passwordRepeat' : {code: 1202, message: '新密码不能与旧密码一致!', status: 200},

  // request 错误类型
  'request.rangeError': { code: 1201, message: '数值越界!', status: 200 },
  'request.referenceError': { code: 1201, message: '非法或不能识别的引用数值!', status: 200 },
  'request.syntaxError': { code: 1201, message: '发生语法解析错误!', status: 200 },
  'request.typeError': { code: 1201, message: '操作数类型错误!', status: 200 },
  'request.URIError': { code: 1201, message: 'URI处理函数使用不当!', status: 200 },
  // 'request.paramError': { code: 1202, message: '请检查您的请求参数!', status: 200 },

  /* 数据库错误类型 */

  'db.queryError': { code: 1300, message: '数据库查询异常', status: 500 },
  'db.insertError': { code: 1301, message: '数据库写入异常', status: 500 },
  'db.updateError': { code: 1302, message: '数据库更新异常', status: 500 },
  'db.deleteError': { code: 1303, message: '数据库删除异常', status: 500 },

  /** 文章错误类型  */
  'ar.notFinddata': { code: 1001, message: '文章被外星人掳走了,快去把它找回来吧！', status: 200 },
  'ar.HasExist': { code: 1001, message: '文章已存在，无需再次发布', status: 200 },
  'ar.nodata': { code: 1001, message: '已经没有更多的数据了', status: 200 }
}

module.exports = function(errorName, params) {
  
  if(defines[errorName]) {
    let result = {
        code: defines[errorName].code,
        message: defines[errorName].message,
        status: defines[errorName].status
    }

    params.forEach(element => {
        result.message = (result.message).replace('%s', element)
    });

    return result
  }
  return {
    code: 1000,
    message: 'Server Error,Please Call Me!',
    status: 500
  }
}
